package mobi.greenplate.theweb.bootstrap;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import mobi.greenplate.theweb.domain.Address;
import mobi.greenplate.theweb.domain.Community;
import mobi.greenplate.theweb.domain.Transport;
import mobi.greenplate.theweb.domain.User;
import mobi.greenplate.theweb.repository.AddressRepository;
import mobi.greenplate.theweb.repository.CommunityRepository;
import mobi.greenplate.theweb.repository.FriendRepository;
import mobi.greenplate.theweb.repository.LocationRepository;
import mobi.greenplate.theweb.repository.OfferRideRepository;
import mobi.greenplate.theweb.repository.RequestRideRepository;
import mobi.greenplate.theweb.repository.TransportRepository;
import mobi.greenplate.theweb.repository.UserDetailRepository;
import mobi.greenplate.theweb.repository.UserRepository;
import org.springframework.stereotype.Service;

/**
 *
 * @author dmakariev
 */
@Service
public class BootstrapTestUser {

    @Inject
    private AddressRepository addressRepository;
    @Inject
    private CommunityRepository communityRepository;
    @Inject
    private FriendRepository friendRepository;
    @Inject
    private LocationRepository locationRepository;
    @Inject
    private OfferRideRepository offerRideRepository;
    @Inject
    private RequestRideRepository requestRideRepository;
    @Inject
    private TransportRepository transportRepository;
    @Inject
    private UserDetailRepository userDetailRepository;
    @Inject
    private UserRepository userRepository;
    
    //"testuser","someFriend"
    public User createNewUser(String userName, String password){
        boolean isTestUserRegistered = userRepository.isUserRegistered(userName);
        if(true==isTestUserRegistered){
            return null;
        }

        final User user = BootstrapDomainFactory.newUser(userName, password);
        User registeredUser = userRepository.save(user);
        return registeredUser;
    }

    @PostConstruct
    public void initializaDataBaseWithTestUser(){
        //set bootstrap.testuser=true
        final String bootStrapTestUser = System.getenv("bootstrap.testuser");
        if(!"true".equals(bootStrapTestUser)){
            return;
        }
        final User owner = createNewUser("testuser", "testuser");
        if(null==owner){
            return;
        }
        final User friendUser = createNewUser("someFriend", "someFriend");
        userDetailRepository.save(BootstrapDomainFactory.newUserDetail(owner),owner);
        userDetailRepository.save(BootstrapDomainFactory.newUserDetail(friendUser),friendUser);
        
        final Address startAddress = addressRepository.save(BootstrapDomainFactory.newAddress_1());
        final Address endAddress = addressRepository.save(BootstrapDomainFactory.newAddress_2());
        final Community community = communityRepository.save(BootstrapDomainFactory.newCommunity(owner),owner);
        friendRepository.save(BootstrapDomainFactory.newFriend(owner, community),owner);
        locationRepository.save(BootstrapDomainFactory.newLocation(owner, endAddress),owner);
        final Transport transport = transportRepository.save(BootstrapDomainFactory.newTransport(owner),owner);
        offerRideRepository.save(BootstrapDomainFactory.newOfferRide(owner, endAddress, startAddress, transport),owner);
        requestRideRepository.save(BootstrapDomainFactory.newRequestRide(friendUser, endAddress, startAddress),owner);
    }

    
    

}
